"""
doc string comes here
"""

from pylab import zeros
from random import randint as randi
from numpy import shape


algorithmSettings = {'chromosomeLength' : 9,
                     'populationSize' : 2,
                     'iterations' : 10000,
                     'crossoverRate' : 0.8,
                     'mutationRate' : 0.05}

class Chromosome:

    def __init__(self,variables):
        self.gCode = [randi(0,1) for i in range(algorithmSettings['chromosomeLength'])]
        self.symbols = variables
        
    

    def value(self):
        result = 0
        for i in range(self.length):
            result += self.gCode[i]*2**(self.length-i-1)
        return result

def joinChromosomes(*args):
    member = [] # Initialising member of the population
    for i in args:
        for j in i.gCode:
            member.append(j)
    return member

def evaluateFitness(ObjectiveFunctions,ChromosomeList,*args):
    varValues ={}
    for j in ChromosomeList:
        varValues[j.symbol] = j.value()

    return eval(ObjectiveFunctions[0],varValues)

def crossover(population):
    crossoverPoint = randi(0,shape(population)[1]-1)
    print crossoverPoint
    for i in range(0,shape(population)[0],2):
        (population[i])[0:crossoverPoint], (population[i+1])[0:crossoverPoint] = (population[i+1])[0:crossoverPoint], (population[i])[0:crossoverPoint]
    return population


def mutate(population):
    mutateMember = randi(0,shape(population)[0]-1)
    mutateBit = randi(0,shape(population)[1])
    print mutateMember,mutateBit
    population[mutateMember][mutateBit] = ((population[mutateMember][mutateBit]+1))%2

def optimize(objectiveFunctions, constraintFunctions, variables):
    nVar = len(variables)
    algorithmSettings['chromosomeLength'] *= len(variables)

    pop = []
    for  memberID in range(algorithmSettings['populationSize']):
        pop.append(Chromosome(variables))
        print pop[memberID].gCode
